package com.myday.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.myday.system.domain.MyTodo;
import com.myday.system.service.IMyTodoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 待办事项Controller
 * 
 * @author xiaofengs
 * @date 2024-05-18
 */
@RestController
@RequestMapping("/system/todo")
public class MyTodoController extends BaseController
{
    @Autowired
    private IMyTodoService myTodoService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询待办事项列表
     */
    @PreAuthorize("@ss.hasPermi('system:todo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyTodo myTodo)
    {
        myTodo.setUserId(getUserId());
        startPage();
        List<MyTodo> list = myTodoService.selectMyTodoList(myTodo);
        return getDataTable(list);
    }

    /**
     * 导出待办事项列表
     */
    @PreAuthorize("@ss.hasPermi('system:todo:export')")
    @Log(title = "待办事项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyTodo myTodo)
    {
        myTodo.setUserId(getUserId());
        List<MyTodo> list = myTodoService.selectMyTodoList(myTodo);
        ExcelUtil<MyTodo> util = new ExcelUtil<MyTodo>(MyTodo.class);
        util.exportExcel(response, list, "待办事项数据");
    }


    @GetMapping(value = "/{todoId}")
    public AjaxResult getInfo(@PathVariable("todoId") Long todoId)
    {
        return success(myTodoService.selectMyTodoByTodoId(todoId));
    }

    @GetMapping(value = "/{todoId}/user")
    public AjaxResult getDetail(@PathVariable("todoId") Long todoId){
        return success((Object) myTodoService.selectUserByTodoId(todoId));
    }


    /**
     * 新增待办事项
     */
    @PreAuthorize("@ss.hasPermi('system:todo:add')")
    @Log(title = "待办事项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyTodo myTodo)
    {
        myTodo.setUserId(getUserId());
        return toAjax(myTodoService.insertMyTodo(myTodo));
    }

    /**
     * 修改待办事项
     */
    @PreAuthorize("@ss.hasPermi('system:todo:edit')")
    @Log(title = "待办事项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyTodo myTodo)
    {
        myTodo.setUserId(getUserId());
        return toAjax(myTodoService.updateMyTodo(myTodo));
    }

    /**
     * 删除待办事项
     */
    @PreAuthorize("@ss.hasPermi('system:todo:remove')")
    @Log(title = "待办事项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{todoIds}")
    public AjaxResult remove(@PathVariable Long[] todoIds)
    {
        return toAjax(myTodoService.deleteMyTodoByTodoIds(todoIds));
    }
}
